package com.rongke.sms;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 功能:  云信留客 HTTP接口 发送短信
 * 说明:	 http://120.55.197.77:1210/Services/MsgSend.asmx/SendMsg
 */
@Service
@Transactional
public class SmsService {

    public InputStream smsCodeByType(String phone,String code){
        String url="http://120.55.197.77:1210/Services/MsgSend.asmx/SendMsg";

        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("userCode", "ZHZBCF"));//ZHZBCF
        nvps.add(new BasicNameValuePair("userPass", "BCF123ault"));
        nvps.add(new BasicNameValuePair("DesNo", phone));
        nvps.add(new BasicNameValuePair("Msg", "尊敬的用户，您的验证码是："+code+"【多空权赢】"));
        nvps.add(new BasicNameValuePair("Channel", "0"));
        return httpPost(url,nvps);  //post请求
//        return post;
    }


    public static InputStream httpPost(String url,List<NameValuePair> params) {
//        String result = "";
        InputStream inputStream = null;
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
            HttpResponse response = httpclient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                inputStream = entity.getContent();


            }
        } catch (Exception e) {
        }
        return inputStream;
    }

    /*public static String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }*/

}
